<?php
/**
 * Created by PhpStorm.
 * User: xh
 * Date: 2021/8/6
 * Time: 10:54
 */
require_once "../config/config.php";
require_once "../model/userDb.php";
$userDb=new userDb();
$name=$_POST["username"];
$pass=md5($_POST["password"]);
$time=time();
try{
    $data=$userDb->login($name,$pass,$time);

    if($data){
        session_start();
        $_SESSION['name']=$name;
        $lifeTime = 20 * 60*60;   //
        setcookie(session_name(), session_id(), time() + $lifeTime, "/");//设置cookie过期时长
        $dat=array(
            'msg'=>'登录成功',
            'xx'=> $_SESSION['name'],
            'code'=>200
        );
    }
    else{
        $dat=array(
            'msg'=>'登录失败',
            'code'=>202,
            'xx'=>$name,
            'pass'=>$pass,
            '$time'=>$time,
            '$data'=>$data
        );
    }
    echo json_encode($dat);

}catch(exception $e) {

    $dat=array(
        'msg'=>'登录失败',
        'code'=>201
    );

    echo json_encode($dat);

}

